package cdu.javaendwork.Controller.Shopping;

import cdu.javaendwork.model.sqlModel.Order;
import cdu.javaendwork.model.sqlModel.User;
import cdu.javaendwork.service.Impl.Shopping.OrderServiceImpl;
import cdu.javaendwork.service.Interface.Shopping.OrderService;
import cdu.javaendwork.util.pageHelp.PageHelper;
import cdu.javaendwork.util.pageHelp.PageInfo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet("/my_orders")
public class OrdersController extends HttpServlet {
    //声明需要提供服务的service以及分页工具
    OrderService orderService = new OrderServiceImpl();
    PageHelper pageHelper = new PageHelper();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //拿到页码
        String sPage=req.getParameter("page");
        int pageSize=3;
        int pageIndex=(sPage == null || sPage.equals("")) ? 1 : Integer.parseInt(sPage);

        //拿到登录用户的id
        HttpSession session = req.getSession();
        User user= (User)session.getAttribute("user");
        int userid = user.getId();
        //查询用户订单
        List<Order> orderList = orderService.filterByUserIdLimit(userid,pageIndex,pageSize);
        //拿到分页信息
        PageInfo pageInfo = pageHelper.filter("Order" , "userid="+userid+" and status=2",pageIndex,pageSize);

        req.setAttribute("orderList",orderList);
        req.setAttribute("pageInfo",pageInfo);

        req.getRequestDispatcher("myOrders.jsp").forward(req,resp);
    }
}
